package com.anan.config;

import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.annotation.Resource;


/**
 * @author anan
 * @date 2023/03/16/20:36
 **/

@Configuration
public class GlobalCorsConfig {

//    @Resource
//    Logger logger;

    @Bean
    public CorsFilter corsFilter() {
        System.out.println("跨域请求拦截。。。");
//        logger.info("跨域允许...");
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许所有域名跨域访问
        corsConfiguration.addAllowedOrigin("*");

        //是否发送Cookie信息
        corsConfiguration.setAllowCredentials(false);
        //放行哪些原始域(头部信息)
        corsConfiguration.addAllowedHeader("*");
        //允许所有请求方法跨域访问
        corsConfiguration.addAllowedMethod("*");

        //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
        corsConfiguration.addExposedHeader("*");

//       corsConfiguration.addExposedHeader("Content-Type");
//       corsConfiguration.addExposedHeader( "X-Requested-With");
//       corsConfiguration.addExposedHeader("accept");
//       corsConfiguration.addExposedHeader("Origin");
//       corsConfiguration.addExposedHeader( "Access-Control-Request-Method");
//       corsConfiguration.addExposedHeader("Access-Control-Request-Headers");


        UrlBasedCorsConfigurationSource urlSource = new UrlBasedCorsConfigurationSource();
        urlSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlSource);
    }
}
